<template>
  <div>
    <el-dialog class="add-car" title="新增/修改车辆" :visible.sync="dialogInfo_to" @close="close" width="450px" append-to-body>
        <el-form :rules="rules" :model="addCarForm" ref="addCarForm">
          
               <el-form-item label="一级分类" :label-width="formLabelWidth">
                  <el-select v-model="addCarForm.categoryId" size="small"  style="width:80%" @change="selectFirstCate($event)">
                    <el-option v-for="item in carFirstCate" 
                      :key="item.categoryId" 
                      :label="item.categoryName" 
                      :value="item.categoryId">
                    </el-option>
                  </el-select>
                </el-form-item>
          
               <el-form-item label="车辆细类" :label-width="formLabelWidth">
                  <el-select v-model="addCarForm.categoryTwoId" size="small" placeholder="请先选择一级分类" style="width:80%">
                    <el-option v-for="item in twoCategory" 
                      :key="item.categoryTwoId" 
                      :label="item.categoryTwoName" 
                      :value="item.categoryTwoId">
                    </el-option>
                  </el-select>
                </el-form-item>
         
              <el-form-item prop="carNumber" label="车辆牌照" :label-width="formLabelWidth">
                <el-input v-model="addCarForm.carNumber" size="small" style="width:80%"></el-input>
              </el-form-item>
         
              <el-form-item label="备注" prop="carReamrks" :label-width="formLabelWidth">
                <el-input v-model="addCarForm.carReamrks" size="small" style="width:80%"></el-input>
              </el-form-item>
      
              <el-form-item label="产品图片" :label-width="formLabelWidth">
                <el-upload
                  action="#"
                  :file-list="fileList"
                  :auto-upload="true"
                  :http-request="uploadCarimg"
                  accept=".jpg,.jpeg,.png,.gif,.bmp,.JPG,.JPEG,.PNG,.GIF,.webp"
                  :on-success="handleAvatarSuccess">
                  <!-- <img v-if="imageUrl" :src="imageUrl" class="prev-img"> -->
                  <el-button size="small" type="primary">上传图片</el-button>
                </el-upload>
              </el-form-item>
       
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="close">取 消</el-button>
          <el-button size="small" type="primary" :loading="submitLoading" @click="submitCarinfo('addCarForm')">确 定</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import { uploadProdImg } from '@/api/prod';
import { GetAllCarCate, AddCarInfo, UpdateCarInfo, GetTwoCateByfirst } from '@/api/car';
import uploadimg from '@/components/uploadimg';
export default {
  name: 'addprod',
  props: ['dialogInfo', 'choseCar', 'selectTwoCate' ],
  components: {
    uploadimg
  },
  data() {
    return {
      fileList: [],
      dialogInfo_to: false,
      submitLoading: false,
      addCarForm: {
        categoryId: "",
        categoryTwoId: '',
        carNumber: "",
        carRemarks: '',
        carImg: "",
      },
      carFirstCate: [],
      twoCategory: [],
      formLabelWidth: '90px',
      rules: {
          carNumber: [
            { required: true, message: '请输入车辆牌照', trigger: 'blur' },
          ],
      }
    }
  },
  computed: {
    storeid() {
      return this.$store.state.store.store_id || getStore();
    },
    main_store() {
      return this.$store.state.store.main_store;
    },
  },
  mounted() {
      this.getAllFirstCate();
  },
  methods: {
    close() {
      this.dialogInfo_to = false;
      this.resetForm();
      this.$emit("update:dialogInfo", false);
    },
    uploadCarimg(param) {
      var formdata = new FormData()
      var file = param.file;
      formdata.append('file', file)
      uploadProdImg(formdata).then(res => {
        if (res.data.code == 1 ) {
          this.$message({
            message: '上传成功',
            type: 'success'
          })
          this.addCarForm.carImg = res.data.data
        }  
      })
    },
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw);
      // this.advertForm.flash_screen = ''
    },
    getAllFirstCate() {
        GetAllCarCate().then( res =>  {
            this.carFirstCate = res.data;
        })
    },
    // 通过一级分类获取二级分类
    selectFirstCate(value) {
      GetTwoCateByfirst(value).then( res => {
        this.twoCategory = res.data;
      })
    },
    resetForm() {
      this.addCarForm.categoryId = '',
      this.addCarForm.categoryTwoId = '',
      this.addCarForm.carNumber = '',
      this.addCarForm.carImg = '',
      this.addCarForm.carRemarks = ''
    },
    submitCarinfo(formName) {
       this.$refs[formName].validate((valid) => {
            if (valid) {
              if (this.choseCar) {
                // 修改商品信息
                let reqData = {
                    carId: this.choseCar.carId,
                    categoryId: this.addCarForm.categoryId,
                    carCategorytwoid: this.addCarForm.categoryTwoId,
                    carNumber: this.addCarForm.carNumber,
                    carRemarks: this.addCarForm.carRemarks,
                    carImg: this.addCarForm.carImg, 
                    deptId: this.storeid,
                }
                UpdateCarInfo(reqData).then(response => {
                    let data = response.data
                    this.$message({
                        message: data.msg,
                        type: 'success'
                    })
                    this.submitLoading = false;
                    this.$refs.addCarForm.resetFields();
                    this.dialogInfo_to = false;
                    this.resetForm();
                    this.$emit("update:dialogInfo", false);
                }).catch(error => {
                    this.submitLoading = false;
                })

              } else {
                // 新增商品信息
                let reqData = {
                    categoryId: this.addCarForm.categoryId,
                    carCategorytwoid: this.addCarForm.categoryTwoId,
                    carNumber: this.addCarForm.carNumber,
                    carRemarks: this.addCarForm.carRemarks,
                    carImg: this.addCarForm.carImg, 
                    deptId: this.storeid,
                }
                this.submitLoading = true;
                AddCarInfo(reqData).then(response => {
                    let data = response.data
                    this.$message({
                        message: data.msg,
                        type: 'success'
                    })
                    this.submitLoading = false;
                    this.$refs.addCarForm.resetFields();
                    this.dialogInfo_to = false;
                    this.resetForm();
                    this.$emit("update:dialogInfo", false);
                }).catch(error => {
                    this.submitLoading = false;
                })
              }
            } else {
              console.log('error submit!!');
              return false;
            }
        });
    }
  },
  watch: {
    dialogInfo(newVal,oldVal) {  
      this.dialogInfo_to = newVal;
    },
    choseCar(newVal) {
      if (newVal) {
        this.selectFirstCate(newVal.categoryId)
        this.addCarForm = newVal;
        this.addCarForm.categoryTwoId = newVal.carCategorytwoid;
      }
    },
    selectTwoCate(newVal) {
      if (newVal) {
        this.addCarForm.categoryId = newVal.categoryId;
        this.selectFirstCate(newVal.categoryId);
        this.addCarForm.categoryTwoId = newVal.categoryTwoId;
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  
</style>